﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Web;
using System.Web.Mvc;
using QuanLyCayXanh.Models;
using System.Data;

namespace QuanLyCayXanh.Controllers
{
    public class ReportController : Controller
    {
        private QuanLyCayXanhContext db = new QuanLyCayXanhContext();

        //
        // GET: /Report/

        public ActionResult Index()
        {
            var reports = db.Reports.Include(r => r.Tree);
            return View(reports.ToList());
        }

        //
        // GET: /Report/Details/5

        public ActionResult Details(int id = 0)
        {
            Report report = db.Reports.Find(id);
            if (report == null)
            {
                return HttpNotFound();
            }
            return PartialView(report);
        }

        //
        // GET: /Report/Create

        public ActionResult Create()
        {
            ViewBag.Tree_Id = new SelectList(db.Trees, "Id", "NameVietNam");
            ViewBag.Street_Id = new SelectList(db.Streets, "Id", "Name");
            ViewBag.Ward_Id = null;
            ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name");
            return View();
        }

        //
        // POST: /Report/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Report report)
        {
            if (ModelState.IsValid)
            {
                if (String.IsNullOrEmpty(report.DateOfReference.ToString()))
                {
                    report.DateOfReference = DateTime.Now;
                }
                db.Reports.Add(report);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Tree_Id = new SelectList(db.Trees, "Id", "NameVietNam", report.Tree_Id);
            return View(report);
        }

        //
        // GET: /Report/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Report report = db.Reports.Find(id);
            if (report == null)
            {
                return HttpNotFound();
            }
            ViewBag.Tree_Id = new SelectList(db.Trees, "Id", "NameVietNam", report.Tree_Id);
            return View(report);
        }

        //
        // POST: /Report/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Report report)
        {
            if (ModelState.IsValid)
            {
                db.Entry(report).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Tree_Id = new SelectList(db.Trees, "Id", "NameVietNam", report.Tree_Id);
            return View(report);
        }

        //
        // GET: /Report/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Report report = db.Reports.Find(id);
            if (report == null)
            {
                return HttpNotFound();
            }
            return View(report);
        }

        //
        // POST: /Report/Delete/5

        [HttpPost, ActionName("Delete")]        
        public ActionResult DeleteConfirmed(int id)
        {
            Report report = db.Reports.Find(id);
            db.Reports.Remove(report);
            db.SaveChanges();
            return Content(Boolean.TrueString);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult GetTrees(int? addressId)
        {
            //QuanLyCayXanhContext db = new QuanLyCayXanhContext();
            //int address_Id = addressId ?? -1;
            //if (address_Id == -1)
            //    return null;
            
            //var list = db.Trees.Where(x => x.Address_Id == address_Id).Select(x => new { x.Id, x.PointX, x.PointY });
            //return Json(list, JsonRequestBehavior.AllowGet);          
            return View();

        }

        [HttpPost, ActionName("GetTree")]     
        public ActionResult GetTree(int? id)
        {
            int Tree_Id = id ?? -1;
            if (Tree_Id == -1)
                return null;
            Tree tree = db.Trees.Find(id);
            if (tree == null)
                return null;
            else
                return PartialView(tree);
        }

    }
}
